﻿-- TODO:DS artistlinkmusicvideo, directorlinkmusicvideo, genrelinkmusicvideo, musicvideo, studiolinkmusicvideo
DELETE
FROM path
WHERE noUpdate IS NULL
AND idPath NOT IN (
    SELECT idPath FROM files
    UNION ALL
    SELECT idPath FROM tvshowlinkpath
);

DELETE
FROM movie
WHERE idFile NOT IN (SELECT idFile FROM files);

DELETE
FROM setlinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM sets
WHERE idSet NOT IN (SELECT idSet FROM setlinkmovie);

DELETE
FROM actorlinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM countrylinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM directorlinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM genrelinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM studiolinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM writerlinkmovie
WHERE idMovie NOT IN (SELECT idMovie FROM movie);

DELETE
FROM country
WHERE idCountry NOT IN (SELECT idCountry FROM countrylinkmovie);

DELETE
FROM settings
WHERE idFile NOT IN (SELECT idFile FROM files);

DELETE
FROM stacktimes
WHERE idFile NOT IN (SELECT idFile FROM files);

DELETE
FROM streamdetails
WHERE idFile NOT IN (SELECT idFile FROM files);

DELETE
FROM episode
WHERE idFile NOT IN (SELECT idFile FROM files);

DELETE
FROM tvshowlinkepisode
WHERE idEpisode NOT IN (SELECT idEpisode FROM episode);

DELETE
FROM tvshow
WHERE idShow NOT IN (SELECT idShow FROM tvshowlinkepisode);

DELETE
FROM tvshowlinkpath
WHERE idShow NOT IN (SELECT idShow FROM tvshow);

DELETE
FROM actors
WHERE idActor NOT IN (
    SELECT idActor FROM actorlinkepisode
    UNION ALL
    SELECT idActor FROM actorlinkmovie
    UNION ALL
    SELECT idActor FROM actorlinktvshow
    UNION ALL
    SELECT idDirector FROM directorlinkepisode
    UNION ALL
    SELECT idDirector FROM directorlinkmovie
    UNION ALL
    SELECT idDirector FROM directorlinkmusicvideo
    UNION ALL
    SELECT idDirector FROM directorlinktvshow
    UNION ALL
    SELECT idWriter FROM writerlinkepisode
    UNION ALL
    SELECT idWriter FROM writerlinkmovie
);

DELETE
FROM genre
WHERE idGenre NOT IN (
    SELECT idGenre FROM genrelinkmovie
    UNION ALL
    SELECT idGenre FROM genrelinkmusicvideo
    UNION ALL
    SELECT idGenre FROM genrelinktvshow
);

DELETE
FROM studio
WHERE idStudio NOT IN (
    SELECT idStudio FROM studiolinkmovie
    UNION ALL
    SELECT idStudio FROM studiolinkmusicvideo
    UNION ALL
    SELECT idStudio FROM studiolinktvshow
);
